home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / kaffe-0.2 / kaffe / soft2.c < prev    next >
C/C++ Source or Header  |  1996-02-19  |  5KB  |  255 lines

  1. /*
  2.  * soft2.c
  3.  * Soft instruction implementations.
  4.  *
  5.  * Copyright (c) 1996 Systems Architecture Research Centre,
  6.  *           City University, London, UK.
  7.  *
  8.  * See the file "license.terms" for information on usage and redistribution
  9.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10.  *
  11.  * Written by Tim Wilkinson <tim@sarc.city.ac.uk>, February 1996.
  12.  */
  13.  
  14. #define ADBG(s)
  15. #define MDBG(s)
  16. #define SDBG(s)
  17.  
  18. #include <stdio.h>
  19. #include <assert.h>
  20. #include <stdarg.h>
  21. #include "gtypes.h"
  22. #include "itypes.h"
  23. #include "object.h"
  24. #include "needs.h"
  25. #include "classMethod.h"
  26. #include "lookup.h"
  27. #include "errors.h"
  28. #include "locks.h"
  29.  
  30. #define    MAXDIMS    8
  31.  
  32. #ifdef NEED_soft_unimplemented
  33. void
  34. soft_unimplemented()
  35. {
  36.     fprintf(stderr, "Unimplemented soft instruction.\n");
  37.     fflush(stderr);
  38.     abort();
  39. }
  40. #endif
  41.  
  42. #ifdef NEED_soft_multianewarray
  43. void
  44. soft_multianewarray(int dims, classes* class, int sizen, ...)
  45. {
  46.     /* dims, class, sizen, ... size1 */
  47.     va_list ap;
  48.     int arraydims[MAXDIMS];
  49.     int i;
  50.     int* arg;
  51.     object* obj;
  52.  
  53.     assert(dims < MAXDIMS);
  54.  
  55.     /* Extract the dimensions into an array */
  56.     va_start(ap, sizen);
  57.     for (i = 0; i < dims; i++) {
  58.         arg = &va_arg(ap, int);
  59.         if (*arg < 0) {
  60.             throwException(NegativeArraySizeException);
  61.         }
  62.         arraydims[dims-(i+1)] = *arg;
  63.     }
  64.     arraydims[i] = 0;
  65.     va_end(ap);
  66.  
  67.     /* Mmm, okay now build the array using the wonders of recursion */
  68.     obj = alloc_multiarray(arraydims, class->name);
  69.  
  70.     /* Return the base object */
  71.     *arg = (int)obj;
  72. }
  73. #endif
  74.  
  75. #ifdef NEED_soft_new
  76. void
  77. soft_new(classes* c)
  78. {
  79.     object* obj;
  80.  
  81.     obj = alloc_object((classes*)c, false);
  82.  
  83. ADBG(    printf("New object of type %s (%d,%x)\n", c->name, c->fsize, obj);
  84.         fflush(stdout);                        )
  85.  
  86.     *(&c) = (classes*)obj;
  87. }
  88. #endif
  89.  
  90. #ifdef NEED_soft_newarray
  91. void
  92. soft_newarray(int type, int size)
  93. {
  94.     object* obj;
  95.  
  96.     if (size < 0) {
  97.         throwException(NegativeArraySizeException);
  98.     }
  99.  
  100.     obj = alloc_array(size, type);
  101.  
  102. ADBG(    printf("New object of %d type (%d,%x)\n", type, size, obj);
  103.     fflush(stdout);                            )
  104.  
  105.     *(&size) = (int)obj;
  106. }
  107. #endif
  108.  
  109. #ifdef NEED_soft_anewarray
  110. void
  111. soft_anewarray(classes* c, int size)
  112. {
  113.     object* obj;
  114.  
  115.     if (size < 0) {
  116.         throwException(NegativeArraySizeException);
  117.     }
  118.  
  119.     obj = alloc_objectarray(size, c->sig);
  120.  
  121. ADBG(    printf("New object array of type %s (%d,%x)\n", c->name, size, obj);
  122.         fflush(stdout);                        )
  123.  
  124.     *(&size) = (int)obj;
  125. }
  126. #endif
  127.  
  128. #ifdef NEED_soft_athrow
  129. void
  130. soft_athrow(object* o)
  131. {
  132.     throwException(o);
  133. }
  134. #endif
  135.  
  136. #if defined(NEED_soft_checkcast) || defined(NEED_soft_instanceof)
  137. bool isInterface(classes*, classes*);
  138. #endif
  139.  
  140. #ifdef NEED_soft_checkcast
  141. void
  142. soft_checkcast(classes* c, object* o)
  143. {
  144.     classes* oc;
  145.     int i;
  146.  
  147.     /* Null can be cast to anything */
  148.     if (o == 0) {
  149.         return;
  150.     }
  151.  
  152.     /* First look down the superclass list */
  153.     for (oc = o->mtable->class; oc != 0; oc = oc->superclass) {
  154.         if (oc == c) {
  155.             return;
  156.         }
  157.     }
  158.  
  159.     /* If this fails, look at the interfaces */
  160.     if (isInterface(o->mtable->class, c)) {
  161.         return;
  162.     }
  163.  
  164.     throwException(ClassCastException);
  165. }
  166.  
  167. #endif
  168.  
  169. #ifdef NEED_soft_instanceof
  170. void
  171. soft_instanceof(classes* c, object* o)
  172. {
  173.     classes* oc;
  174.  
  175.     if (o != 0) {
  176.         for (oc = o->mtable->class; oc != 0; oc = oc->superclass) {
  177.             if (oc == c) {
  178.                 *(&o) = (object*)1;
  179.                 return;
  180.             }
  181.         }
  182.         /* If this fails, look at the interfaces */
  183.         if (isInterface(o->mtable->class, c)) {
  184.             *(&o) = (object*)1;
  185.             return;
  186.         }
  187.         *(&o) = (object*)0;
  188.     }
  189. }
  190. #endif
  191.  
  192. #ifdef NEED_soft_lookupmethod
  193. void
  194. soft_lookupmethod(methodTable* tab, strpair* pair)
  195. {
  196.     void* func;
  197.  
  198.     func = findMethod(tab->class, pair);
  199.  
  200. #if !defined(DEBUG)
  201.     /* We do not fill in the tag in debug mode, so forcing a lookup
  202.      * on every method call.  This can help debugging but makes things
  203.      * go real slow.
  204.      */
  205.     tab->m[pair->hash % MAXMETHOD].tag = pair;
  206. #endif
  207.     tab->m[pair->hash % MAXMETHOD].method = func;
  208.  
  209. MDBG(    printf("Calling %s:%s%s @ 0x%x\n",
  210.         tab->class->name, pair->s1, pair->s2, func);
  211.         fflush(stdout);                        )
  212.  
  213.     if (func == 0) {
  214.         throwException(NoSuchMethodError);
  215.     }
  216. }
  217. #endif
  218.  
  219. #ifdef NEED_soft_monitorenter
  220. void
  221. soft_monitorenter(object* obj)
  222. {
  223. SDBG(    printf("Monitor enter %x\n", obj);            )
  224.     lockMutex(obj);
  225. }
  226. #endif
  227.  
  228. #ifdef NEED_soft_monitorexit
  229. void
  230. soft_monitorexit(object* obj)
  231. {
  232. SDBG(    printf("Monitor exit %x\n", obj);            )
  233.     unlockMutex(obj);
  234. }
  235. #endif
  236.  
  237. #if defined(NEED_soft_checkcast) || defined(NEED_soft_instanceof)
  238. /*
  239.  * Does this interface match the class.  Recuse down interface.
  240.  *  list if not.
  241.  */
  242. bool
  243. isInterface(classes* ic, classes* c)
  244. {
  245.     int i;
  246.  
  247.     for (i = 0; i < ic->interface_len; i++) {
  248.         if (ic->interface[i] == c || isInterface(ic->interface[i], c)) {
  249.             return (true);
  250.         }
  251.     }
  252.     return (false);
  253. }
  254. #endif
  255.